﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MediaDataModel;
using System.Xml;
using MediaDataModel.Common.Other;

namespace MediaClient.Management
{
    public partial class SystemSettingForm : BaseForm
    {
        Button tempBtn = new Button();
        Label tempLb = null;
        string tempStr = "";
        List<KeyValue> kvList = new List<KeyValue>();
        CCTV6Entities cctv6 = new CCTV6Entities();

        public SystemSettingForm()
        {
            InitializeComponent();
            this.pnlMain.MouseMove += base.pnlMainForm_MouseMove;
            this.pnlMain.MouseUp += base.pnlMainForm_MouseUp;
            this.pnlMain.MouseDown += base.pnlMainForm_MouseDown;
            tempBtn = btnAccount;
        }

        private void lbClose_Click(object sender, EventArgs e)
        {
            Label currentLbl = sender as Label;
            if (currentLbl != null)
            {
                switch (currentLbl.Name)
                {
                    //case "lblMini":
                    //    this.WindowState = FormWindowState.Minimized;
                    //    break;
                    case "lbClose":
                        this.Close();
                        break;
                }
            }
        }

        private void lbClose_MouseMove(object sender, MouseEventArgs e)
        {
            Label currentLbl = sender as Label;
            if (currentLbl != null)
            {
                switch (currentLbl.Name)
                {
                    //case "lblMini":
                    //    //lblMini.Image = global::MediaClient.Properties.Resources.窗口最小化按钮_1;
                    //    break;
                    case "lbClose":
                        lbClose.Image = global::MediaClient.Properties.Resources.窗口关闭按钮_1;
                        break;

                }
            }
        }

        private void lbClose_MouseLeave(object sender, EventArgs e)
        {
            Label currentLbl = sender as Label;
            if (currentLbl != null)
            {
                switch (currentLbl.Name)
                {
                    //case "lblMini":
                    //    lblMini.Image = global::MediaClient.Properties.Resources.窗口最小化按钮;
                    //    break;
                    case "lbClose":
                        lbClose.Image = global::MediaClient.Properties.Resources.窗口关闭按钮;
                        break;

                }
            }
        }

        private void btnControl_MouseMove(object sender, MouseEventArgs e)
        {
            Button btn = sender as Button;
            if (btn != null)
            {
                switch (btn.Name)
                {
                    case "btnAccount":
                        btnAccount.BackgroundImage = Properties.Resources.账号管理_1;
                        break;
                    case "btnClient":
                        btnClient.BackgroundImage = Properties.Resources.客户管理_1;
                        break;
                    case "btnVideo":
                        btnVideo.BackgroundImage = Properties.Resources.视频管理_1;
                        break;
                    case "btnPower":
                        btnPower.BackgroundImage = Properties.Resources.权限管理_1;
                        break;
                    case "btnPassWord":
                        btnPassWord.BackgroundImage = Properties.Resources.密码管理_1;
                        break;
                }
            }
        }

        private void btnControl_MouseLeave(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            if (btn != null)
            {
                switch (btn.Name)
                {
                    case "btnAccount":
                        btnAccount.BackgroundImage = Properties.Resources.账号管理;
                        break;
                    case "btnClient":
                        btnClient.BackgroundImage = Properties.Resources.客户管理;
                        break;
                    case "btnVideo":
                        btnVideo.BackgroundImage = Properties.Resources.视频管理;
                        break;
                    case "btnPower":
                        btnPower.BackgroundImage = Properties.Resources.权限管理;
                        break;
                    case "btnPassWord":
                        btnPassWord.BackgroundImage = Properties.Resources.密码管理;
                        break;
                    case "btnAccOK":
                    case "btnClientOK":
                    case "btnVideoOK":
                    case "btnPowerOK":
                    case "btnPwdOK":
                        btn.BackgroundImage = Properties.Resources.确定;
                        break;
                    case "btnAccCancel":
                    case "btnClientCancel":
                    case "btnVideoCancel":
                    case "btnPowerCancel":
                    case "btnPwdCancel":
                        btn.BackgroundImage = Properties.Resources.重置;
                        break;
                }
            }

        }

        private void btnControl_Click(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            if (btn != null)
            {
                switch (btn.Name)
                {
                    case "btnAccount":
                        tempLb = null;
                        tempStr = "";
                        ClearTextBox(pnlAccountContent);
                        cboAccRoles.SelectedIndex = 0;
                        pnlAccRightContent.Controls.Clear();
                        SetControl(btn);
                        pnlAccount.Visible = true;
                        btnAccount.MouseMove -= btnControl_MouseMove;
                        btnAccount.MouseLeave -= btnControl_MouseLeave;
                        btnAccount.BackgroundImage = Properties.Resources.账号管理_当前;
                        BindRightContent();
                        break;
                    case "btnClient":
                        tempLb = null;
                        tempStr = "";
                        ClearTextBox(pnlClientContent);
                        pnlClientRightContent.Controls.Clear();
                        SetControl(btn);
                        pnlClient.Visible = true;
                        btnClient.MouseMove -= btnControl_MouseMove;
                        btnClient.MouseLeave -= btnControl_MouseLeave;
                        btnClient.BackgroundImage = Properties.Resources.客户管理_当前;
                        BindRightContent();
                        break;
                    case "btnVideo":
                        tempLb = null;
                        tempStr = "";
                        ClearTextBox(pnlVideoContent);
                        cboVideoType.SelectedIndex = 0;
                        pnlVideoRightContent.Controls.Clear();
                        SetControl(btn);
                        pnlVideo.Visible = true;
                        btnVideo.MouseMove -= btnControl_MouseMove;
                        btnVideo.MouseLeave -= btnControl_MouseLeave;
                        btnVideo.BackgroundImage = Properties.Resources.视频管理_当前;
                        BindRightContent();
                        break;
                    case "btnPower":
                        tempLb = null;
                        tempStr = "";
                        pnlPowerRightContent.Controls.Clear();
                        SetControl(btn);
                        pnlPower.Visible = true;
                        btnPower.MouseMove -= btnControl_MouseMove;
                        btnPower.MouseLeave -= btnControl_MouseLeave;
                        btnPower.BackgroundImage = Properties.Resources.权限管理_当前;
                        BindRightContent();
                        break;
                    case "btnPassWord":
                        tempLb = null;
                        tempStr = "";
                        SetControl(btn);
                        pnlPwd.Visible = true;
                        btnPassWord.MouseMove -= btnControl_MouseMove;
                        btnPassWord.MouseLeave -= btnControl_MouseLeave;
                        btnPassWord.BackgroundImage = Properties.Resources.密码管理_当前;
                        BindRightContent();
                        break;
                    case "btnAccOK":
                        CheckedTextBox(btn);
                        if (tempLb != null)
                        {
                            User user = cctv6.Users.Where(item => item.ID == tempLb.Tag).FirstOrDefault();
                            if (user != null)
                            {
                                if (txtAccPwd.Text.Trim() == txtAccPwdConfirm.Text.Trim())
                                {
                                    user.Name = txtAccNickname.Text.Trim();
                                    if (user.Password != txtAccPwd.Text.Trim())
                                    {
                                        if (txtAccPwd.Text.Trim() == txtAccPwdConfirm.Text.Trim())
                                        {
                                            user.Password = CommonUtility.Utility.MD5Encrypt(txtAccPwd.Text.Trim());
                                        }
                                        else
                                        {
                                            MessageBox.Show("两次密码不一致", "消息提示");
                                            return;
                                        }
                                    }
                                    user.Email = txtAccEmail.Text.Trim();
                                    txtAccUserName.ReadOnly = false;
                                    cctv6.SaveChanges();
                                    if (cboAccRoles.SelectedIndex != 0)
                                    {
                                        UpdateUserRoleObject(user.ID);
                                    }
                                    else
                                    {
                                        DeleteUserRoleObject(user.ID);
                                    }
                                    MessageBox.Show("修改成功！", "消息提示");
                                }
                                else
                                {
                                    MessageBox.Show("两次密码不一致！", "消息提示");
                                }
                            }
                        }
                        else
                        {
                            User user = new User();
                            if (string.IsNullOrEmpty(txtAccUserName.Text.Trim()))
                            {
                                MessageBox.Show("用户名不能为空！", "消息提示");
                                return;
                            }
                            if ((cctv6.Users.Where(item => item.ID == tempLb.Tag).FirstOrDefault()) == null)
                            {
                                MessageBox.Show("用户名已存在！", "消息提示");
                                return;
                            }
                            if (txtAccPwd.Text.Trim() == txtAccPwdConfirm.Text.Trim())
                            {
                                user.ID = txtAccUserName.Text.Trim();
                                user.Name = txtAccNickname.Text.Trim();
                                user.Password = CommonUtility.Utility.MD5Encrypt(txtAccPwd.Text.Trim());
                                user.Email = txtAccEmail.Text.Trim();
                                user.IsActive = true;
                                cctv6.Users.AddObject(user);
                                cctv6.SaveChanges();
                                if (cboAccRoles.SelectedIndex != 0)
                                {
                                    AddUserRoleObject(user.ID);
                                }
                                MessageBox.Show("新增成功！", "消息提示");
                            }
                            else
                            {
                                MessageBox.Show("两次密码不一致！", "消息提示");
                            }
                        }
                        btnControl_Click(btnAccount, new EventArgs());
                        break;
                    case "btnClientOK":
                        CheckedTextBox(btn);
                        if (tempLb != null)
                        {
                            Customer oCustomer = cctv6.Customers.Where(item => item.ID == tempStr).FirstOrDefault();
                            if (oCustomer != null)
                            {
                                oCustomer.Address = txtCliAddress.Text.Trim();
                                oCustomer.CompanyName = txtCliCompanyName.Text.Trim();
                                oCustomer.ContractName = txtCliLinkMan.Text.Trim();
                                oCustomer.Email = txtCliEmail.Text.Trim();
                                oCustomer.Phone = txtCliPhone.Text.Trim();
                                txtCliUserName.ReadOnly = false;
                                cctv6.SaveChanges();
                                MessageBox.Show("修改成功！", "消息提示");
                            }
                        }
                        else
                        {
                            User oUser = new User();
                            oUser.ID = txtCliUserName.Text.Trim();
                            oUser.Name = txtCliCompanyName.Text.Trim();
                            oUser.IsActive = true;
                            cctv6.Users.AddObject(oUser);
                            Customer oCustomer = new Customer();
                            oCustomer.ID = txtCliUserName.Text.Trim();
                            oCustomer.Address = txtCliAddress.Text.Trim();
                            oCustomer.CompanyName = txtCliCompanyName.Text.Trim();
                            oCustomer.ContractName = txtCliLinkMan.Text.Trim();
                            oCustomer.Email = txtCliEmail.Text.Trim();
                            oCustomer.Phone = txtCliPhone.Text.Trim();
                            Customer oCus = cctv6.Customers.OrderByDescending(item => item.WaterMarkerNumber).FirstOrDefault();
                            if (oCus == null)
                            {
                                oCustomer.WaterMarkerNumber = "001";
                            }
                            else
                            {
                                string tempWM = (int.Parse(oCus.WaterMarkerNumber) + 1).ToString();
                                if (tempWM.Length < 3)
                                {
                                    for (int i = 0; i <= 3 - tempWM.Length; i++)
                                    {
                                        tempWM = "0" + tempWM;
                                    }
                                }
                                oCustomer.WaterMarkerNumber = tempWM;
                            }
                            cctv6.Customers.AddObject(oCustomer);
                            cctv6.SaveChanges();
                            MessageBox.Show("新增成功！", "消息提示");
                        }
                        btnControl_Click(btnClient, new EventArgs());
                        break;
                    case "btnVideoOK":
                        CheckedTextBox(btn);
                        int maxWeight = int.Parse(cctv6.MetaDataNames.Max(item => item.Weight).ToString());
                        if (tempLb != null)
                        {
                            int tempInt = (Int32)tempLb.Tag;
                            MetaDataName mdn = cctv6.MetaDataNames.Where(item => item.ID == tempInt).FirstOrDefault();
                            if (mdn != null)
                            {
                                mdn.Name = txtVideoName.Text.Trim();
                                if (!string.IsNullOrEmpty(txtWeight.Text.Trim()))
                                {
                                    mdn.Weight = int.Parse(txtWeight.Text.Trim());
                                }
                                if (cboVideoType.SelectedIndex != 0)
                                {
                                    mdn.DataType = short.Parse(cboVideoType.SelectedValue.ToString());
                                    cctv6.SaveChanges();
                                }
                            }
                        }
                        else
                        {
                            MetaDataName mdn = new MetaDataName();
                            if (!string.IsNullOrEmpty(txtVideoName.Text.Trim()))
                            {
                                mdn.Name = txtVideoName.Text.Trim();
                                mdn.Weight = maxWeight + 10;
                                if (cboVideoType.SelectedIndex != 0 && !string.IsNullOrEmpty(txtWeight.Text.Trim()))
                                {
                                    mdn.DataType = short.Parse(cboVideoType.SelectedValue.ToString());
                                    cctv6.MetaDataNames.AddObject(mdn);
                                    cctv6.SaveChanges();
                                    MessageBox.Show("新增成功！", "消息提示");
                                }
                                else
                                {
                                    MessageBox.Show("必须要选择数据类型！", "消息提示");
                                }

                            }
                            else
                            {
                                MessageBox.Show("字段名或权重值不能为空！", "消息提示");
                            }

                        }
                        btnControl_Click(btnVideo, new EventArgs());
                        break;
                    case "btnPowerOK":
                        CheckedTextBox(btn);
                        if (tempLb != null)
                        {
                            short temp = (short)tempLb.Tag;
                            Role role = cctv6.Roles.Where(item => item.ID == temp).FirstOrDefault();
                            if (role != null)
                            {
                                string tempS = "";
                                foreach (Control c in pnlPowerContent.Controls)//pnlPowerContent
                                {
                                    if (c is CheckBox)
                                    {
                                        if ((c as CheckBox).Checked == true)
                                        {
                                            tempS += ((c as CheckBox).Tag.ToString()) + ";";
                                        }
                                    }
                                }
                                role.ModuleList = tempS.Remove(tempS.Length - 1);
                            }
                            cctv6.SaveChanges();
                            MessageBox.Show("修改成功", "消息提示");
                        }
                        else
                        {
                            Role role = new Role();
                            if (!string.IsNullOrEmpty(txtPowerRolrName.Text.Trim()))
                            {
                                role.Name = txtPowerRolrName.Text.Trim();
                            }
                            else
                            {
                                MessageBox.Show("角色名不能为空", "消息提示");
                            }
                            string tempS = "";
                            foreach (Control c in pnlPowerContent.Controls)
                            {
                                if (c is CheckBox)
                                {
                                    if ((c as CheckBox).Checked == true)
                                    {
                                        tempS += ((c as CheckBox).Tag.ToString()) + ";";
                                    }
                                }
                            }
                            role.ModuleList = tempS.Remove(tempS.Length - 1);
                            cctv6.Roles.AddObject(role);
                            cctv6.SaveChanges();
                            MessageBox.Show("新增成功", "消息提示");
                        }
                        txtPowerRolrName.ReadOnly = false;
                        btnControl_Click(btnPower, new EventArgs());
                        break;
                    case "btnPwdOK":
                        break;
                    case "btnAccCancel":
                        ClearTextBox(pnlAccountContent);
                        //txtAccUserName.ReadOnly = false;
                        cboAccRoles.SelectedIndex = 0;
                        tempLb = null;
                        break;
                    case "btnClientCancel":
                        ClearTextBox(pnlClientContent);
                        //txtCliUserName.ReadOnly = false;
                        tempLb = null;
                        break;
                    case "btnVideoCancel":
                        ClearTextBox(pnlVideoContent);
                        cboVideoType.SelectedIndex = 0;
                        tempLb = null;
                        break;
                    case "btnPowerCancel":
                        foreach (Control c in pnlPowerContent.Controls)
                        {
                            if (c is TextBox)
                            {
                                c.Text = "";
                            }
                            if (c is CheckBox)
                            {
                                (c as CheckBox).Checked = false;
                            }
                        }
                        txtPowerRolrName.ReadOnly = false;
                        tempLb = null;
                        break;
                    case "btnPwdCancel":
                        break;
                }
            }
        }

        private void CheckedTextBox(Button btn)
        {
            switch (btn.Name)
            {
                case "btnAccOK":
                    if (string.IsNullOrEmpty(txtAccUserName.Text.Trim()))
                    {
                        MessageBox.Show("用户名不能为空！");
                        return;
                    }
                    break;
                case "btnClientOK":
                    if (string.IsNullOrEmpty(txtCliUserName.Text.Trim()))
                    {
                        MessageBox.Show("用户名不能为空！");
                        return;
                    }
                    if (string.IsNullOrEmpty(txtCliCompanyName.Text.Trim()))
                    {
                        MessageBox.Show("公司名称不能为空！");
                        return;
                    }
                    break;
                case "btnVideoOK":
                    if (string.IsNullOrEmpty(txtVideoName.Text.Trim()))
                    {
                        MessageBox.Show("字段名不能为空！");
                        return;
                    }
                    break;
                case "btnPowerOK":
                    if (string.IsNullOrEmpty(txtPowerRolrName.Text.Trim()))
                    {
                        MessageBox.Show("角色名不能为空！");
                        return;
                    }
                    break;
            }
        }

        private void DeleteUserRoleObject(string userID)
        {
            UserRole ur = cctv6.UserRoles.Where(item => item.UserID == userID).FirstOrDefault();
            if (ur != null)
            {
                cctv6.UserRoles.DeleteObject(ur);
                cctv6.SaveChanges();
            }
        }

        private void UpdateUserRoleObject(string userID)
        {
            UserRole ur = cctv6.UserRoles.Where(item => item.UserID == userID).FirstOrDefault();
            if (ur != null)
            {
                ur.RoleID = (short)cboAccRoles.SelectedValue;
                cctv6.SaveChanges();
            }
        }

        private void AddUserRoleObject(string userID)
        {
            UserRole ur = new UserRole();
            ur.RoleID = (short)cboAccRoles.SelectedValue;
            ur.UserID = userID;
            cctv6.UserRoles.AddObject(ur);
            cctv6.SaveChanges();
        }

        private void ClearTextBox(Panel pnl)
        {
            foreach (Control oCon in pnl.Controls)
            {
                if (oCon is TextBox)
                {
                    (oCon as TextBox).ReadOnly = false;
                    oCon.Text = "";
                }
            }
        }

        private void ModifyVideoManageInfo()
        {
            try
            {
                List<MetaDataName> mdnList = cctv6.MetaDataNames.ToList();
                foreach (MetaDataName mdn in mdnList)
                {
                    if (tempLb.Text.Trim() == mdn.Name)
                    {
                        mdn.Name = txtVideoName.Text.Trim();
                        mdn.DataType = short.Parse(cboVideoType.SelectedValue.ToString());
                    }
                }
                cctv6.SaveChanges();
            }
            catch
            { }
        }

        private void SetControl(Button btn)
        {
            if (tempBtn != null && tempBtn != btn)
            {
                switch (tempBtn.Name)
                {
                    case "btnAccount":
                        tempBtn.BackgroundImage = Properties.Resources.账号管理;
                        btnAccount.MouseLeave += new EventHandler(btnControl_MouseLeave);
                        btnAccount.MouseMove += new MouseEventHandler(btnControl_MouseMove);
                        pnlAccount.Visible = false;
                        break;
                    case "btnClient":
                        tempBtn.BackgroundImage = Properties.Resources.客户管理;
                        btnClient.MouseLeave += new EventHandler(btnControl_MouseLeave);
                        btnClient.MouseMove += new MouseEventHandler(btnControl_MouseMove);
                        pnlClient.Visible = false;
                        break;
                    case "btnVideo":
                        tempBtn.BackgroundImage = Properties.Resources.视频管理;
                        btnVideo.MouseLeave += new EventHandler(btnControl_MouseLeave);
                        btnVideo.MouseMove += new MouseEventHandler(btnControl_MouseMove);
                        pnlVideo.Visible = false;
                        break;
                    case "btnPower":
                        tempBtn.BackgroundImage = Properties.Resources.权限管理;
                        btnPower.MouseLeave += new EventHandler(btnControl_MouseLeave);
                        btnPower.MouseMove += new MouseEventHandler(btnControl_MouseMove);
                        pnlPower.Visible = false;
                        break;
                    case "btnPassWord":
                        tempBtn.BackgroundImage = Properties.Resources.密码管理;
                        btnPassWord.MouseLeave += new EventHandler(btnControl_MouseLeave);
                        btnPassWord.MouseMove += new MouseEventHandler(btnControl_MouseMove);
                        pnlPwd.Visible = false;
                        break;
                }
            }
            tempBtn = btn;
        }

        private void SystemSettingForm_Load(object sender, EventArgs e)
        {
            ;
            BindRightContent();
            BindComboBoxData();
            BindRoleDataToComboBox();
        }

        private void BindRoleDataToComboBox()
        {
            List<Role> roleList = new List<Role>(); ;
            Role r = new Role();
            r.Name = "--请选择--";
            roleList.Add(r);
            foreach (Role role in cctv6.Roles.ToList())
            {
                roleList.Add(role);
            }
            cboAccRoles.DataSource = roleList;
            cboAccRoles.DisplayMember = "Name";
            cboAccRoles.ValueMember = "ID";
        }

        private void BindRightContent()
        {
            if (tempBtn != null)
            {
                switch (tempBtn.Name)
                {
                    case "btnAccount":
                        List<User> userList = cctv6.Users.ToList();
                        foreach (User oUser in userList)
                        {
                            Label lbNull = new Label();
                            lbNull.AutoSize = false;
                            lbNull.Dock = DockStyle.Top;
                            lbNull.Size = new Size(131, 2);
                            lbNull.BackColor = Color.Transparent;
                            pnlAccRightContent.Controls.Add(lbNull);

                            Panel pnl = new Panel();
                            pnl.Dock = DockStyle.Top;
                            pnl.BackColor = Color.LightGray;
                            pnl.Size = new Size(131, 23);
                            pnlAccRightContent.Controls.Add(pnl);

                            Label lbImg = new Label();
                            lbImg.Location = new Point(0, 0);
                            lbImg.Size = new Size(23, 23);
                            lbImg.Image = Properties.Resources.用户;
                            pnl.Controls.Add(lbImg);

                            Label lbTxt = new Label();
                            lbTxt.Text = oUser.Name;
                            lbTxt.Size = new Size(112, 23);
                            lbTxt.Location = new Point(23, 0);
                            lbTxt.Tag = oUser.ID;
                            lbTxt.TextAlign = ContentAlignment.MiddleLeft;
                            lbTxt.ContextMenuStrip = cmsRightContent;
                            lbTxt.MouseUp += new MouseEventHandler(lbTxt_MouseUp);
                            lbTxt.MouseClick += new MouseEventHandler(lbTxt_MouseClick);
                            pnl.Controls.Add(lbTxt);
                        }
                        break;
                    case "btnClient":
                        List<Customer> customerList = cctv6.Customers.ToList();
                        foreach (Customer c in customerList)
                        {
                            Label lbNull = new Label();
                            lbNull.AutoSize = false;
                            lbNull.Dock = DockStyle.Top;
                            lbNull.Size = new Size(131, 2);
                            lbNull.BackColor = Color.Transparent;
                            pnlClientRightContent.Controls.Add(lbNull);

                            Panel pnl = new Panel();
                            pnl.Dock = DockStyle.Top;
                            pnl.BackColor = Color.LightGray;
                            pnl.Size = new Size(131, 23);
                            pnlClientRightContent.Controls.Add(pnl);

                            Label lbImg = new Label();
                            lbImg.Location = new Point(0, 0);
                            lbImg.Size = new Size(23, 23);
                            lbImg.Image = Properties.Resources.客户;
                            pnl.Controls.Add(lbImg);

                            Label lbTxt = new Label();
                            lbTxt.Text = c.CompanyName;
                            lbTxt.Size = new Size(112, 23);
                            lbTxt.Location = new Point(23, 0);
                            lbTxt.Tag = c.ID;
                            lbTxt.TextAlign = ContentAlignment.MiddleLeft;
                            lbTxt.ContextMenuStrip = cmsRightContent;
                            lbTxt.MouseUp += new MouseEventHandler(lbTxt_MouseUp);
                            lbTxt.MouseClick += new MouseEventHandler(lbTxt_MouseClick);
                            pnl.Controls.Add(lbTxt);
                        }
                        break;
                    case "btnVideo":
                        List<MetaDataName> mdnList = cctv6.MetaDataNames.OrderByDescending(item=>item.Weight).ToList();
                        foreach (MetaDataName mdn in mdnList)
                        {
                            Label lbNull = new Label();
                            lbNull.AutoSize = false;
                            lbNull.Dock = DockStyle.Top;
                            lbNull.Size = new Size(131, 2);
                            lbNull.BackColor = Color.Transparent;
                            pnlVideoRightContent.Controls.Add(lbNull);

                            Panel pnl = new Panel();
                            pnl.Dock = DockStyle.Top;
                            pnl.BackColor = Color.LightGray;
                            pnl.Size = new Size(131, 23);
                            pnlVideoRightContent.Controls.Add(pnl);

                            Label lbImg = new Label();
                            lbImg.Location = new Point(0, 0);
                            lbImg.Size = new Size(23, 23);
                            lbImg.Image = Properties.Resources.字段;
                            pnl.Controls.Add(lbImg);

                            Label lbTxt = new Label();
                            lbTxt.Text = mdn.Name;
                            lbTxt.Size = new Size(112, 23);
                            lbTxt.Location = new Point(23, 0);
                            lbTxt.TextAlign = ContentAlignment.MiddleLeft;
                            lbTxt.Tag = mdn.ID;
                            lbTxt.ContextMenuStrip = cmsRightContent;
                            lbTxt.MouseUp += new MouseEventHandler(lbTxt_MouseUp);
                            lbTxt.MouseClick += new MouseEventHandler(lbTxt_MouseClick);
                            pnl.Controls.Add(lbTxt);
                        }
                        break;
                    case "btnPower":
                        List<Role> roleList = cctv6.Roles.ToList();
                        foreach (Role c in roleList)
                        {
                            Label lbNull = new Label();
                            lbNull.AutoSize = false;
                            lbNull.Dock = DockStyle.Top;
                            lbNull.Size = new Size(131, 2);
                            lbNull.BackColor = Color.Transparent;
                            pnlPowerRightContent.Controls.Add(lbNull);

                            Panel pnl = new Panel();
                            pnl.Dock = DockStyle.Top;
                            pnl.BackColor = Color.LightGray;
                            pnl.Size = new Size(131, 23);
                            pnlPowerRightContent.Controls.Add(pnl);

                            Label lbImg = new Label();
                            lbImg.Location = new Point(0, 0);
                            lbImg.Size = new Size(23, 23);
                            lbImg.Image = Properties.Resources.用户;
                            pnl.Controls.Add(lbImg);

                            Label lbTxt = new Label();
                            lbTxt.Text = c.Name;
                            lbTxt.TextAlign = ContentAlignment.MiddleLeft;
                            lbTxt.Size = new Size(112, 23);
                            lbTxt.Location = new Point(23, 0);
                            lbTxt.Tag = c.ID;
                            lbTxt.ContextMenuStrip = cmsRightContent;
                            lbTxt.MouseUp += new MouseEventHandler(lbTxt_MouseUp);
                            lbTxt.MouseClick += new MouseEventHandler(lbTxt_MouseClick);
                            pnl.Controls.Add(lbTxt);
                        }
                        break;
                    case "btnPassWord":
                        break;
                }
            }
        }

        void lbTxt_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                tempLb = sender as Label;
            }
        }

        private void BindComboBoxData()
        {

            KeyValue kv = new KeyValue();
            kv.Key = "--请选择--";
            kv.Value = "0";
            kvList.Add(kv);
            InitializeMetadataDataType();
            cboVideoType.Items.Clear();
            cboVideoType.DataSource = kvList;
            cboVideoType.DisplayMember = "Key";
            cboVideoType.ValueMember = "Value";
        }

        public void InitializeMetadataDataType()
        {
            XmlDocument xmlDataType = new XmlDocument();
            xmlDataType.Load(BaseForm.systemConfigFile);
            XmlNodeList dataTypeList = xmlDataType.DocumentElement.SelectNodes("metaData/dataType/type");
            foreach (XmlNode typeNode in dataTypeList)
            {
                KeyValue kv = new KeyValue();
                kv.Key = typeNode.Attributes["name"].Value;
                kv.Value = typeNode.Attributes["value"].Value;
                kvList.Add(kv);
            }
        }

        void lbTxt_MouseClick(object sender, MouseEventArgs e)
        {
            Label lb = sender as Label;
            if (e.Button == MouseButtons.Left)
            {
                if (lb != null)
                {
                    tempLb = lb;
                    switch (tempBtn.Name)
                    {
                        case "btnAccount":
                            User user = cctv6.Users.Where(item => item.ID == lb.Tag).FirstOrDefault();
                            if (user != null)
                            {
                                txtAccEmail.Text = user.Email;
                                txtAccPwd.Text = user.Password;
                                txtAccPwdConfirm.Text = user.Password;
                                txtAccUserName.Text = user.ID;
                                txtAccNickname.Text = user.Name;
                                UserRole ur = cctv6.UserRoles.Where(item => item.UserID == user.ID).FirstOrDefault();
                                if (ur != null)
                                {
                                    for (int i = 0; i < cboAccRoles.Items.Count; i++)
                                    {
                                        if (((Role)cboAccRoles.Items[i]).ID == ur.RoleID)
                                        {
                                            cboAccRoles.SelectedItem = cboAccRoles.Items[i];
                                        }
                                    }
                                }
                                tempStr = user.ID;
                                txtAccUserName.ReadOnly = true;
                                if (user.ID == "admin" || user.ID == "systemOwer")
                                {
                                    txtAccEmail.ReadOnly = true;
                                    txtAccNickname.ReadOnly = true;
                                }
                            }
                            break;
                        case "btnClient":
                            Customer customer = cctv6.Customers.Where(item => item.ID == lb.Tag).FirstOrDefault();
                            if (customer != null)
                            {
                                txtCliCompanyName.Text = customer.CompanyName;
                                txtCliAddress.Text = customer.Address;
                                txtCliUserName.Text = customer.ID;
                                txtCliEmail.Text = customer.Email;
                                txtCliLinkMan.Text = customer.ContractName;
                                txtCliPhone.Text = customer.Phone;
                                txtCliWatermark.Text = customer.WaterMarkerNumber;
                                tempStr = customer.ID;
                                txtCliUserName.ReadOnly = true;
                                if (customer.IsOwner == true)
                                {
                                    txtCliCompanyName.ReadOnly = true;
                                    txtCliAddress.ReadOnly = true;
                                    txtCliEmail.ReadOnly = true;
                                    txtCliLinkMan.ReadOnly = true;
                                    txtCliPhone.ReadOnly = true;
                                    txtCliWatermark.ReadOnly = true;
                                }
                            }
                            break;
                        case "btnVideo":
                            int tempInt = (Int32)tempLb.Tag;
                            MetaDataName mdn = cctv6.MetaDataNames.Where(item => item.ID == tempInt).FirstOrDefault();
                            if (mdn != null)
                            {
                                txtVideoName.Text = mdn.Name;
                                txtWeight.Text = mdn.Weight.ToString();
                                foreach (var item in cboVideoType.Items)
                                {
                                    if ((item as KeyValue).Value == mdn.DataType.ToString())
                                    {
                                        cboVideoType.SelectedItem = item;
                                        break;
                                    }
                                }

                                if (mdn.DataType == 4 || mdn.DataType == 6)
                                {
                                    lbVideoData.Visible = true;
                                    txtVideoData.Visible = true;
                                }
                                else
                                {
                                    lbVideoData.Visible = false;
                                    txtVideoData.Visible = false;
                                }
                            }

                            break;
                        case "btnPower":
                            foreach (Control c in pnlPowerContent.Controls)
                            {
                                if (c is CheckBox)
                                {
                                    (c as CheckBox).Checked = false;
                                }
                            }
                            short sh = (short)lb.Tag;
                            Role r = cctv6.Roles.Where(item => item.ID == sh).FirstOrDefault();
                            if (r != null)
                            {
                                txtPowerRolrName.Text = r.Name;
                                string[] strList = r.ModuleList.Split(';');
                                for (int i = 0; i < strList.Length; i++)
                                {
                                    foreach (Control control in pnlPowerContent.Controls)
                                    {
                                        if (control is CheckBox)
                                        {
                                            if (control.Tag.ToString() == strList[i])
                                            {
                                                (control as CheckBox).Checked = true;
                                            }
                                        }
                                    }
                                }
                            }
                            txtPowerRolrName.ReadOnly = true;
                            break;
                    }
                }
            }

        }

        private void btnControl_MouseEnter(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            if (btn != null)
            {
                switch (btn.Name)
                {
                    case "btnAccOK":
                    case "btnClientOK":
                    case "btnVideoOK":
                    case "btnPowerOK":
                    case "btnPwdOK":
                        btn.BackgroundImage = Properties.Resources.确定_1;
                        break;
                    case "btnAccCancel":
                    case "btnClientCancel":
                    case "btnVideoCancel":
                    case "btnPowerCancel":
                    case "btnPwdCancel":
                        btn.BackgroundImage = Properties.Resources.重置_1;
                        break;
                }
            }
        }

        private void cboVideoType_SelectionChangeCommitted(object sender, EventArgs e)
        {
            if (cboVideoType.SelectedValue.ToString() == "4" || cboVideoType.SelectedValue.ToString() == "6")
            {
                lbVideoData.Visible = true;
                txtVideoData.Visible = true;
            }
            else
            {
                lbVideoData.Visible = false;
                txtVideoData.Visible = false;
            }
        }

        private void cmsRightContent_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

            if (tempBtn != null && tempLb != null)
            {
                switch (tempBtn.Name)
                {
                    case "btnAccount":
                        string user_ID = tempLb.Tag.ToString();
                        if (user_ID == "admin" || user_ID == "systemOwer")
                        {
                            MessageBox.Show("该用户不可被删除！", "消息提示");
                        }
                        else
                        {
                            User oUser = cctv6.Users.Where(item => item.ID == user_ID).FirstOrDefault();
                            if (oUser != null)
                            {
                                DeleteUserRoleObject(user_ID);
                                if (MessageBox.Show("尚有与该用户关联的数据，你确定删除该字段及其相关数据吗？", "消息提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                                {
                                    if (cctv6.cctv_sp_DeleteUser(user_ID) == 0)
                                    {
                                        MessageBox.Show("删除成功！", "消息提示");
                                    }
                                    else
                                    {
                                        MessageBox.Show("删除失败！", "消息提示");
                                    }
                                }
                                //cctv6.Users.DeleteObject(oUser);
                                //cctv6.SaveChanges();
                                //DeleteUserRoleObject(user_ID);
                            }
                        }
                        break;
                    case "btnClient":
                        string cus_ID = tempLb.Tag.ToString();
                        Customer oCustomer = cctv6.Customers.Where(item => item.ID == cus_ID).FirstOrDefault();
                        if (oCustomer.IsOwner == true)
                        {
                            MessageBox.Show("该客户不可被删除！", "消息提示");
                        }
                        else
                        {
                            if (oCustomer != null)
                            {
                                User relateUser = cctv6.Users.Where(item => item.ID == cus_ID).FirstOrDefault();
                                if (relateUser != null)
                                {
                                    DeleteUserRoleObject(cus_ID);

                                    if (MessageBox.Show("尚有与该用户关联的数据，你确定删除该字段及其相关数据吗？", "消息提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                                    {
                                        if (cctv6.cctv_sp_DeleteUser(cus_ID) == 0)
                                        {
                                            MessageBox.Show("删除成功！", "消息提示");
                                        }
                                        else
                                        {
                                            MessageBox.Show("删除失败！", "消息提示");
                                        }
                                    }

                                }
                                //cctv6.Customers.DeleteObject(oCustomer);
                                //cctv6.SaveChanges();
                            }
                        }
                        break;
                    case "btnVideo":
                        int mdn_Id = int.Parse(tempLb.Tag.ToString());
                        MetaDataName mdn = cctv6.MetaDataNames.Where(item => item.ID == mdn_Id).FirstOrDefault();
                        try
                        {
                            if (mdn != null)
                            {
                                List<MetaDataRecord> mdrList = cctv6.MetaDataRecords.Where(item => item.MetaDataID == mdn_Id).ToList();
                                if (mdrList.Count > 0)
                                {
                                    if (MessageBox.Show("尚有与该字段关联的数据，你确定删除该字段及其相关数据吗？", "消息提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                                    {
                                        if (cctv6.cctv_sp_DeleteMetaRecordData(mdn_Id) == 0)
                                        {
                                            MessageBox.Show("删除成功！", "消息提示");
                                        }
                                        else
                                        {
                                            MessageBox.Show("删除失败！", "消息提示");
                                        }
                                    }
                                }
                                //cctv6.MetaDataNames.DeleteObject(mdn);
                                //cctv6.SaveChanges();
                            }
                        }
                        catch (Exception myE)
                        {
                            throw myE;
                        }
                        break;
                    case "btnPower":
                        int role_ID = int.Parse(tempLb.Tag.ToString());
                        Role oRole = cctv6.Roles.Where(item => item.ID == role_ID).FirstOrDefault();
                        if (oRole != null)
                        {
                            List<UserRole> urList = cctv6.UserRoles.Where(item => item.RoleID == role_ID).ToList();
                            foreach (UserRole ur in urList)
                            {
                                if (ur != null)
                                {
                                    cctv6.UserRoles.DeleteObject(ur);
                                }
                            }
                            cctv6.Roles.DeleteObject(oRole);
                            cctv6.SaveChanges();
                        }
                        break;
                }
                btnControl_Click(tempBtn, new EventArgs());
            }
            else
            {
                MessageBox.Show("数据错误！");
            }
        }

    }
}
